package com.xiaohong.goods.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohong.goods.pojo.Sku;
import com.xiaohong.goods.service.SkuService;
import entity.Result;
import entity.StatusCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "Sku接口文档")
@RestController
@RequestMapping("/sku")
@CrossOrigin
public class SkuController {

    @Autowired
    private SkuService skuService;

    /***
     * 分页条件搜索实现Sku
     * @param sku
     * @param page
     * @param size
     * @return
     */
    @Operation(summary = "条件分页查询Sku",description = "所需权限：")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@RequestBody(required = false) @Parameter(name = "Sku对象",required = true) Sku sku, @PathVariable int page, @PathVariable  int size){
        //调用SkuService实现分页条件查询Sku
        Page pageInfo = skuService.findPage(sku, page, size);
        return new Result<Page>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 分页搜索实现Sku
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @Operation(summary = "分页查询Sku",description = "所需权限：")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用SkuService实现分页查询Sku
        Page pageInfo = skuService.findPage(page, size);
        return new Result<Page>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索Sku数据
     * @param sku
     * @return
     */
    @Operation(summary = "多条件查询Sku",description = "所需权限：")
    @PostMapping(value = "/search" )
    public Result<List<Sku>> findList(@RequestBody(required = false) @Parameter(name = "Sku对象",required = true) Sku sku){
        //调用SkuService实现条件查询Sku
        List<Sku> list = skuService.findList(sku);
        return new Result<List<Sku>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据Id删除sku数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID删除Sku",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true)
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable String id){
        //调用SkuService实现根据ID删除
        skuService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 根据ID修改Sku数据
     * @param sku
     * @param id
     * @return
     */
    @Operation(summary = "根据ID修改Sku",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true)
    @PutMapping(value = "/{id}" )
    public Result update(@RequestBody @Parameter(name = "Sku对象",required = true) Sku sku, @PathVariable String id){
        // 补充id
        sku.setId(id);
        //调用SkuService实现修改Sku
        skuService.update(sku);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增Sku数据
     * @param sku
     * @return
     */
    @Operation(summary = "添加Sku",description = "所需权限：")
    @PostMapping
    public Result add(@RequestBody  @Parameter(name = "Sku对象",required = true) Sku sku){
        //调用SkuService实现添加Sku
        skuService.add(sku);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询Sku数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID查询Sku",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true)
    @GetMapping("/{id}")
    public Result<Sku> findById(@PathVariable String id){
        //调用SkuService实现根据ID查询Sku
        Sku sku = skuService.findById(id);
        return new Result<Sku>(true,StatusCode.OK,"查询成功",sku);
    }

    /***
     * 查询Sku全部数据
     * @return
     */
    @Operation(summary = "查询所有Sku",description = "所需权限：")
    @GetMapping
    public Result<List<Sku>> findAll(){
        //调用SkuService实现查询所有Sku
        List<Sku> list = skuService.findAll();
        return new Result<List<Sku>>(true, StatusCode.OK,"查询成功",list) ;
    }

    /***
     * 查询全部状态正常Sku数据
     * @return
     */
    @Operation(summary = "查询全部状态正常Sku数据",description = "所需权限：")
    @GetMapping("/market")
    public Result<List<Sku>> findAllMarket(){
        //调用SkuService实现查询所有Sku
        List<Sku> list = skuService.findAllMarket();
        return new Result<List<Sku>>(true, StatusCode.OK,"查询成功",list) ;
    }

}
